package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.TcaseBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.TcaseListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Tcaseiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.TcaseService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class TcaseModel implements Tcaseiface {
    private TcaseService tcaseService;
    private Retrofit retrofit;
    private static final  String BASE_URL=
            "http://amicool.neusoft.edu.cn/";
    public TcaseModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(
                        GsonConverterFactory.create())
                .build();

    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final TcaseListener tcaseListener) {
        tcaseService =retrofit
                .create(TcaseService.class);
        Call<List<TcaseBean>> call=tcaseService
                .getResult(mod,page,sessionID);
        call.enqueue(new Callback<List<TcaseBean>>() {
            @Override
            public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                tcaseListener.onResponse(response.body());
                else
                tcaseListener.onFail("error");
            }

            @Override
            public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                tcaseListener.onFail("error");
            }
        });
    }
}
